var axios = require('axios');
var cheerio = require('cheerio');
var fs = require('fs');
var api = axios.create({
    baseURL: 'https://crm.xlingtong.com/',
    withCredentials: true,
    headers: {
        'Cookie': '	ARRAffinity=538d7bc3e0e7918b39ee61fff69ec81c7fd859772a7c96cac512f5f1dcb01894; ASP.NET_SessionId=5jzqauvws1g3uoogdmi3flr4; .xlingtongv2=3B6445462FAEA955B1D0B3698FEA83BE1D02CAD99FE0921898BC04E357BDE1629FFD5F1F3896801BD1492F94F768BFA48CA147F387C6181E33BF2A543F42DC14D8F83D2882A1F625807DF34838D610596EF7CD120034A8FE32B9F4E9E8597FD1138A4FE1AA0F8B794C77C955F54A2F36FF9F7599952128AEE0CF25149C1469F7386B1E373AA706FA6A661B60DF786A4D12FAED9F781117162348FCB2EBE49DEC909A2B818673F90F0C231E8E93EA01965549C13AD88980DA5B31DD9FE2A45DF3191BAA5AF056F9F66CBE453780D00DC7CFF1B588B692CB397060E84EA2D38391FAB681EF0B89C109263C827F2B8D010BCA31FCAAAD361248B4CA5D67FDE7ADF2; __RequestVerificationToken=q9nDc5LB4Tvnd8ATK6xoSIxFZXz_GnaxGiW_lfmqnmR7s-J0s9vgB4UQxmwwJe6aOVvOEa1V5g7Ci9pNJpj5gYX3EgRfTKIFKAQZmTNzBxo1'
    }
});
var $ = "";
var $$ = "";
var List = [];
async function getData(page_index){
   let item_id = await api.get('Platform/CustomerConsult',{
        params: {
            pageIndex: page_index,
        }
    }).then(res => {
        //console.log(res.data);
        $ = cheerio.load(res.data);
        //console.log($.html());
        // console.log($('.table-scrollable tbody').html());
        let id = "";        
        $('.table-scrollable tbody tr').each((i,e) =>{
            // console.log($(e).find('td').length);
            $(e).find('td').each((j,el)=>{
                if(j == 0){
                    id = $(el).find('input').attr('value');
                    return;
                }
                // switch (j) {
                //     case 0:
                //         item.id = $(el).find('input').attr('value');
                //         break;
                //     case 1:
                //         item.user_name = $(el).find('a').text();
                //         break;
                //     case 2:
                //         item.phone = $(el).text().trim();
                //         break;
                //     case 3:
                //         item.type = $(el).text().trim();
                //         break;
                //     case 4:
                //         item.content = $(el).find('a').text();
                //         break;
                //     case 5:
                //         item.creator = $(el).text().trim();
                //         break;
                //     case 6:
                //         item.date = $(el).html().trim();                        
                //         break; 
                //     default:
                //         break;
                // }
            })
            // List.push(item);
        })
        return id;        
        // console.log(List);
    })
    await getDetail(item_id);
}
async function getDetail(id) {
    console.log(id);
    await api.get('Platform/CustomerConsult/Details/'+id)
    .then(res =>{
        $$ = cheerio.load(res.data);
        var item ={};
        item.id = id;
        $$('.form-group .form-control-static').each((i,el) =>{
            switch (i) {
                case 0:
                    item.CustomerId = $(el).text().trim();
                    break;
                case 1:
                    item.ConsultMode = $(el).text().trim();
                    break;
                case 2:
                    item.ConsultStage = $(el).text().trim();
                    break;
                case 3:
                    item.ConsultDate = $(el).text().trim();
                    break;
                case 4:
                    item.ConsultationLength = $(el).text().trim();
                    break;
                case 5:
                    item.NextConsultDate = $(el).text().trim();
                    break;
                case 6:
                    item.IntentionLevel = $(el).text().trim();
                    break;
                case 7:
                    item.StartDate = $(el).text().trim();
                    break;
                case 8:
                    item.EndDate = $(el).text().trim();
                    break;
                case 9:
                    item.ConsultContent = $(el).text().trim();
                    break;
                case 10:
                    item.Remark = $(el).text().trim();
                    break;
                default:
                    break;
            }
        })
        // console.log(item);
        List.push(item);

    })
}
async function main(){
    let page_size = 100;
    let page_count = 1025;
    for (let index = 1; index <= page_count; index++) {
        console.log('page: ' + index);
        await getData(index);
        if(index%page_size==0){
            let file_name = (index-page_size+1)+'-'+index;
            await fs.writeFile('./data/page'+ file_name +'.txt', JSON.stringify(List), { 'flag': 'a' }, function(err) {
                if (err) {
                    throw err;
                }
                List = [];
                console.log('saved.');
            });
            continue;
        }
        if(index == page_count){
            let file_name = (page_count - index%page_size) + '-' + page_count;
            await fs.writeFile('./data/page'+ file_name +'.txt', JSON.stringify(List), { 'flag': 'a' }, function(err) {
                if (err) {
                    throw err;
                }
                List = [];
                console.log('saved.');
            });
            return;
        }
    }
}
main();

